# coding=utf-8
import  os,xlrd,sys,io,xlwt,DegreeConvert

excelPath = 'E:\\PythonProjects\\africa2\\africa\doc\\africa\\data\\result_union.xls'
excelFile=xlrd.open_workbook(excelPath)

workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.add_sheet('sheet1')
worksheet.write(0, 0, label='races')
worksheet.write(0, 1, label='country')
sheet=excelFile.sheet_by_index(0)

dic={}
wRowIndex =1
for i in range(0,sheet.nrows):
    row = sheet.row_values(i)
    countryName = row[3].strip()
    raceName = row[5].strip()
    if not raceName:
        continue
    elif  dic.has_key(raceName):
        if countryName in dic[raceName]:
            continue
        else:
            dic[raceName].append(countryName)
    else:
        dic[raceName] =[countryName]

print len(dic.keys())
for keys, values in dic.items():
    for value in values:
        if not value.strip() and len(values)>1 :
            continue
        worksheet.write(wRowIndex, 0, label=keys)
        worksheet.write(wRowIndex, 1, label=value)
        wRowIndex = wRowIndex + 1
workbook.save('E:\\PythonProjects\\africa2\\africa\\doc\\africa\\data\\export_union.xls')
